package org.tity.commons.encrypt;

import org.tity.commons.lang.BytesUtil;

import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;
import net.jpountz.lz4.LZ4SafeDecompressor;

public class CompressUtil {
	
	
	/**
     * @param srcByte 原始数据
     * @return 压缩后的数据
     */
    public static byte[] compressFast(byte[] srcByte) {
        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4Compressor compressor = factory.fastCompressor();
        byte[] temp=compressor.compress(srcByte);
        byte[] result=new byte[temp.length+4];
        byte[] lens=BytesUtil.int2bytes(srcByte.length); 
        System.arraycopy(lens, 0, result, 0, 4);
		System.arraycopy(temp, 0, result, 4, temp.length);
        return result;
    }

    /**
     * @param compressorByte 压缩后的数据
     * @return 解压缩后的数据
     */
    public static byte[] decompress(byte[] compressorByte) {
    	byte[] lens=new byte[4];
    	System.arraycopy(compressorByte, 0, lens, 0, 4);
    	int srcLength=BytesUtil.bytes2int(lens);
    	byte[] data=new byte[compressorByte.length-4];
    	System.arraycopy(compressorByte, 4, data, 0, data.length);
        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        return decompressor.decompress(data, srcLength);
    }
    
    
    
    /**
     * @param srcByte 原始数据
     * @return 压缩后的数据
     */
    public static byte[] compressHigh(byte[] srcByte) {
        LZ4Factory factory = LZ4Factory.fastestInstance();
        LZ4Compressor compressor = factory.highCompressor();
        byte[] temp=compressor.compress(srcByte);
        byte[] result=new byte[temp.length+4];
        byte[] lens=BytesUtil.int2bytes(srcByte.length); 
        System.arraycopy(lens, 0, result, 0, 4);
		System.arraycopy(temp, 0, result, 4, temp.length);
        return result;
    }

    
   
    
	
	 
	 
//	public static void main(String[] args) throws Exception {
//		
//		 
//		byte[] bb=FileUtil.readBytes(new File("C:\\Users\\Administrator\\Documents\\Tencent Files\\190868487\\FileRecv\\ext-addons-6.5.3-trial\\ext-addons-6.5.3\\packages\\d3\\d3.js"));
////		System.out.println(bb.length);
//		bb=compressHigh(bb);
////		System.out.println(bb.length);
//		FileUtil.writeBytes("C:\\2.data", bb);
//		
//		bb=decompress(bb);
////		
////		bb=decompressFast(bb);
//		FileUtil.writeBytes("C:\\2.1.data", bb);
//	}
	
	public static void main(String[] args) throws Exception {
		LZ4Factory factory = LZ4Factory.fastestInstance();

		byte[] data = "12345345234572".getBytes("UTF-8");
//		byte[] bb=FileUtil.readBytes(new File("C:\\Users\\Administrator\\Documents\\Tencent Files\\190868487\\FileRecv\\ext-addons-6.5.3-trial\\ext-addons-6.5.3\\packages\\d3\\d3.js"));
//		data=bb;
		final int decompressedLength = data.length;
		System.out.println("A1:"+decompressedLength);
		
		// compress data
		LZ4Compressor compressor = factory.fastCompressor();
		int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
		byte[] compressed = new byte[maxCompressedLength];
		int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
		System.out.println("A2:"+compressedLength);
		
		// decompress data
		// - method 1: when the decompressed length is known
		LZ4FastDecompressor decompressor = factory.fastDecompressor();
		byte[] restored = new byte[decompressedLength];
		int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
		// compressedLength == compressedLength2
		System.out.println("B1:"+compressedLength2);

		// - method 2: when the compressed length is known (a little slower)
		// the destination buffer needs to be over-sized
		LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
		int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
		// decompressedLength == decompressedLength2
		System.out.println("B2:"+decompressedLength2);
		
		 
	}
	
	
}
